using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using System.Globalization;
using VSSDKAssist.Enums;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateEventSubscription
{
	/// <summary>
    /// Custom page for CreateEventSubscriptionCP recipe
	/// </summary>
	public partial class CreateEventSubscriptionCP : WizardPageBase
	{

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateEventSubscriptionCP"/> class.
		/// </summary>
		public CreateEventSubscriptionCP()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateEventSubscriptionCP"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
        public CreateEventSubscriptionCP(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the VS event.
		/// </summary>
		/// <value>The VS event.</value>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value"), RecipeArgument]
		public VSEvent VSEvent
		{
			get { return (VSEvent)veVsEvent.Value; }
			set
			{
			}
		}

		/// <summary>
		/// Gets or sets the command GUID.
		/// </summary>
		/// <value>The command GUID.</value>
		[RecipeArgument]
		public string CommandGuid
		{
			get { return (string)veCommandGuid.Value; }
			set
			{
				if(value != null)
				{
					veCommandGuid.TextBox.Text = value.ToString();
				}
			}
		}

		/// <summary>
		/// Gets or sets the command id.
		/// </summary>
		/// <value>The command id.</value>
		[RecipeArgument]
		public string CommandId
		{
			get { return (string)veCommandId.Value; }
			set
			{
				if(value != null)
				{
					veCommandId.TextBox.Text = value.ToString();
				}
			}
		}

		/// <summary>
		/// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
		/// Search for all the arguments that this Wizard gather
		/// If all of them are not null, then the Wizard can finish
		/// </summary>
		/// <value></value>
		public override bool IsDataValid
		{
			get
			{
				errorProvider.SetError(veCommandGuid, string.Empty);
				errorProvider.SetError(veCommandId, string.Empty);

				if(DictionaryService.GetValue("VSEvent") != null)
				{
					VSEvent vsEvent = (VSEvent)DictionaryService.GetValue("VSEvent");

					if(vsEvent == VSEvent.CommandEvents)
					{
						bool ret = true;

						if(DictionaryService.GetValue("CommandGuid") == null)
						{
							errorProvider.SetError(veCommandGuid, Properties.Resources.InvalidValue);
							ret = false;
						}

						if(DictionaryService.GetValue("CommandId") == null)
						{
							errorProvider.SetError(veCommandId, Properties.Resources.InvalidValue);
							ret = false;
						}

						if(!ret)
						{
							return false;
						}
					}
				}

				return base.IsDataValid;
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veVsEvent.BeginInit();
					this.veVsEvent.ValueType = typeof(VSEvent);
					base.Site.Container.Add(veVsEvent);
					this.veVsEvent.ConverterInstance = new EnumConverter(typeof(VSEvent));
					this.veVsEvent.EndInit();

					this.veCommandGuid.BeginInit();
					this.veCommandGuid.ValueType = typeof(string);
					base.Site.Container.Add(veCommandGuid);
					this.veCommandGuid.ConverterInstance = new CommandGuidConverter();
					this.veCommandGuid.EndInit();

					this.veCommandId.BeginInit();
					this.veCommandId.ValueType = typeof(string);
					base.Site.Container.Add(veCommandId);
					this.veCommandId.ConverterInstance = new CommandIdConverter();
					this.veCommandId.EndInit();
				}
			}
		}

        private void CreateEventSubscriptionCP_Load(object sender, EventArgs e)
		{
			SetControls();
		}

		private void SetControls()
		{
			lblCommandGuid.Visible = false;
			veCommandGuid.Visible = false;
			lblCommandId.Visible = false;
			veCommandId.Visible = false;
			pctCommandGuid.Visible = false;
		}

		private void veCommandId_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veCommandId.ConverterInstance != null)
			{
				errorProvider.SetError(veCommandId, string.Empty);

				if(veCommandId.Value != null)
				{
					DictionaryService.SetValue("CommandId", veCommandId.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("CommandId", null);
					errorProvider.SetError(veCommandId, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("CommandId", veCommandId.Value.ToString());
			}
		}

		private void veCommandGuid_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veCommandGuid.ConverterInstance != null)
			{
				errorProvider.SetError(veCommandGuid, string.Empty);

				if(veCommandGuid.Value != null)
				{
					DictionaryService.SetValue("CommandGuid", ((Guid)veCommandGuid.Value).ToString("D").ToUpper(CultureInfo.CurrentCulture));
				}
				else
				{
					DictionaryService.SetValue("CommandGuid", null);
					errorProvider.SetError(veCommandGuid, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("CommandGuid", ((Guid)veCommandGuid.Value).ToString("D").ToUpper(CultureInfo.CurrentCulture));
			}
		}

		private void veVsEvent_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			VSEvent vsEvent = (VSEvent)veVsEvent.Value;
			DictionaryService.SetValue("VSEvent", vsEvent);

			lblCommandGuid.Visible = (vsEvent == VSEvent.CommandEvents);
			veCommandGuid.Visible = (vsEvent == VSEvent.CommandEvents);
			lblCommandId.Visible = (vsEvent == VSEvent.CommandEvents);
			veCommandId.Visible = (vsEvent == VSEvent.CommandEvents);
			pctCommandGuid.Visible = (vsEvent == VSEvent.CommandEvents);
		}

		private void pctCommandGuid_Click(object sender, EventArgs e)
		{
			Bitmap bitmap = CustomPageHelper.GetBitmapResource("EnableVSIPLogging");

			if(bitmap != null)
			{
				InfoForm infoForm = new InfoForm(bitmap);

				infoForm.ShowDialog();
			}
		}
	}
}